<template>
  <div>
    <h2>有效优惠券列表</h2>
    <table class="table table-bordered">
      <tr>
        <td>ID</td>
        <td>标题</td>
        <td>类型</td>
        <td>数量</td>
        <td>开始时间</td>
        <td>结束时间</td>
        <td>操作</td>
      </tr>
      <tr v-for="item in list">
        <td>{{ item.dId }}</td>
        <td>{{ item.dTitle }}</td>
        <td>{{ item.dtName }}</td>
        <td>{{ item.dNumber }}</td>
        <td>{{ item.dBeginTime }}</td>
        <td>{{ item.dEndTime }}</td>
        <td>
          <input type="button" value="曾送" @click="add(item.dId)" />
        </td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, onMounted } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
import { useUserStore } from '../stores/user';

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();

let data: any = reactive({
  list: []
});

let { list } = toRefs(data);

onMounted(() => {
  getValidDiscountList();
});

//赠送  == 增加
const add = (id:any) => {
  let obj = {
    mId: route.query.id,
    dId: id,
  };

  axios({
    url: '/api/Test/PostAddMemberDiscount',
    method: 'post',
    data: obj
  }).then(res=>{
    console.log(res);
    if(res.data.data == 1){
      alert("赠送成功");
      getValidDiscountList()
    }else if(res.data.data ==-1){
      alert("赠送过了，不能再送了");
      return
    }else{
      alert("赠送失败");
      return
    }
    
  })
  .catch(err=>{
    console.log(err);
    
  })
};

//获取有效优惠券列表

const getValidDiscountList = () => {
  axios({
    url: '/api/Test/GetValidDiscountList',
    method: 'get'
  })
    .then((res) => {
      console.log(res);
      list.value = res.data.data;
    })
    .catch((err) => {
      console.log(err);
    });
};
</script>

<style scoped></style>
